# -*- coding: utf-8 -*-
# createTime: 2018/12/19 14:10
import logging
from datetime import datetime

from django.utils import timezone
from django.core.serializers.json import DjangoJSONEncoder


def get_logger(name=None):
    return logging.getLogger(f'KSM.{name}')


def get_object_or_none(model, **kwargs):
    try:
        obj = model.objects.get(**kwargs)
    except model.DoesNotExist:
        return None
    return obj


def get_request_param(request, param, default=None):
    return request.POST.get(param) or request.GET.get(param, default)


class DjangoJSONOverRideEncoder(DjangoJSONEncoder):
    def default(self, o):

        if isinstance(o, datetime):
            if timezone.is_aware(o):
                o = timezone.localtime(o)
            r = o.isoformat(' ')[:19]
            return r
        return super().default(o)
